package com.ruoyi.web.controller.business;

import com.ruoyi.business.bo.HcxCouponAddBo;
import com.ruoyi.business.bo.HcxCouponEditBo;
import com.ruoyi.business.bo.HcxCouponQueryBo;
import com.ruoyi.business.service.IHcxCouponService;
import com.ruoyi.business.vo.HcxCouponVo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 优惠券(平台发送全商家通用)Controller
 *
 * @author wsdz
 * @date 2022-08-17
 */
@Api(value = "优惠券(平台发送全商家通用)控制器", tags = {"优惠券(平台发送全商家通用)管理"})
@AllArgsConstructor
@RestController
@RequestMapping("/business/coupon")
public class HcxCouponController extends BaseController {

    private final IHcxCouponService iHcxCouponService;

    /**
     * 查询优惠券(平台发送全商家通用)列表
     */
    @ApiOperation("查询优惠券(平台发送全商家通用)列表")
    @PreAuthorize("@ss.hasPermi('business:coupon:list')")
    @GetMapping("/list")
    public TableDataInfo<HcxCouponVo> list(@Validated HcxCouponQueryBo bo) {
        return iHcxCouponService.queryPageList(bo);
    }

    /**
     * 导出优惠券(平台发送全商家通用)列表
     */
    @ApiOperation("导出优惠券(平台发送全商家通用)列表")
    @PreAuthorize("@ss.hasPermi('business:coupon:export')")
    @Log(title = "优惠券(平台发送全商家通用)", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<Void> export(@Validated HcxCouponQueryBo bo) {
        List<HcxCouponVo> list = iHcxCouponService.queryList(bo);
        ExcelUtil<HcxCouponVo> util = new ExcelUtil<>(HcxCouponVo.class);
        return util.exportExcel(list, "优惠券(平台发送全商家通用)");
    }

    /**
     * 获取优惠券(平台发送全商家通用)详细信息
     */
    @ApiOperation("获取优惠券(平台发送全商家通用)详细信息")
    @PreAuthorize("@ss.hasPermi('business:coupon:query')")
    @GetMapping("/{id}")
    public AjaxResult<HcxCouponVo> getInfo(@NotNull(message = "主键不能为空")
                                           @PathVariable("id") Long id) {
        return AjaxResult.success(iHcxCouponService.queryById(id));
    }

    /**
     * 新增优惠券(平台发送全商家通用)
     */
    @ApiOperation("新增优惠券(平台发送全商家通用)")
    @PreAuthorize("@ss.hasPermi('business:coupon:add')")
    @Log(title = "优惠券(平台发送全商家通用)", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated @RequestBody HcxCouponAddBo bo) {
        return toAjax(iHcxCouponService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改优惠券(平台发送全商家通用)
     */
    @ApiOperation("修改优惠券(平台发送全商家通用)")
    @PreAuthorize("@ss.hasPermi('business:coupon:edit')")
    @Log(title = "优惠券(平台发送全商家通用)", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated @RequestBody HcxCouponEditBo bo) {
        return toAjax(iHcxCouponService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除优惠券(平台发送全商家通用)
     */
    @ApiOperation("删除优惠券(平台发送全商家通用)")
    @PreAuthorize("@ss.hasPermi('business:coupon:remove')")
    @Log(title = "优惠券(平台发送全商家通用)", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                   @PathVariable Long[] ids) {
        return toAjax(iHcxCouponService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

    /**
     * 导入优惠券(平台发送全商家通用)数据
     */
    @ApiOperation("导入优惠券(平台发送全商家通用)数据")
    @ApiImplicitParam(name = "file", value = "文件", required = true, dataType = "file")
    @PreAuthorize("@ss.hasPermi('business:coupon:import')")
    @Log(title = "优惠券(平台发送全商家通用)", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult<Void> importData(MultipartFile file) {

        ExcelUtil<HcxCouponVo> util = new ExcelUtil<>(HcxCouponVo.class);
        List<HcxCouponVo> hcxCouponList;
        try {
            hcxCouponList = util.importExcel(file.getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException("文件解析异常！message：" + e.getMessage());
        }
        String message = iHcxCouponService.importHcxCoupon(hcxCouponList);
        return AjaxResult.success(message);

    }

    /**
     * 下载模板
     *
     * @return AjaxResult 模板文件
     */
    @ApiOperation("下载模板")
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate() {
        ExcelUtil<HcxCouponVo> util = new ExcelUtil<>(HcxCouponVo.class);
        return util.importTemplateExcel("优惠券(平台发送全商家通用)数据");
    }
}
